package com.engine4cloud.tdf.supervision.convert;

import com.engine4cloud.tdf.supervision.dto.DcpgCrimeNameDTO;
import com.engine4cloud.tdf.supervision.dto.JzdxCrimeInfoDTO;
import com.engine4cloud.tdf.supervision.dto.create.JzdxCrimeInfoCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.DcpgInvCrimeNameResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.DictSelectResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxCrimeInfoResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxResultDTO;
import com.engine4cloud.tdf.supervision.entity.JzdxCrimeInfoEntity;
import com.engine4cloud.tdf.supervision.entity.JzdxEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Mapper
public interface JzdxCrimeInfoConvert {
    JzdxCrimeInfoConvert INSTANCE = Mappers.getMapper(JzdxCrimeInfoConvert.class);

    JzdxCrimeInfoCreateDTO create(JzdxEntity entity, List<JzdxCrimeInfoDTO> crimeNameList);

    DcpgCrimeNameDTO create(String zm);

    JzdxCrimeInfoResultDTO convert(JzdxCrimeInfoEntity s);

    default JzdxCrimeInfoDTO convert(DcpgInvCrimeNameResultDTO s) {
        if (s == null) {
            return null;
        }

        JzdxCrimeInfoDTO crimeInfo = new JzdxCrimeInfoDTO();
        crimeInfo.setJtzm(s.getZm());
        crimeInfo.setJtzmc(s.getZmmc());
        return crimeInfo;
    }

    default JzdxCrimeInfoEntity convert(DictSelectResultDTO s, JzdxCrimeInfoCreateDTO dto) {
        JzdxCrimeInfoEntity entity = new JzdxCrimeInfoEntity();
        entity.setFzlx(s.getPcode());
        entity.setJtzm(s.getCode());
        entity.setRybh(dto.getRybh());
        entity.setTenantId(dto.getTenantId());
        return entity;
    }

    /**
     * 设置罪名名称和犯罪类型名称
     *
     * @param crimeInfoList 犯罪明细列表
     * @param crimeNameList 具体罪名列表
     * @param crimeTypeList 犯罪类型列表
     */
    default void convertCrimeInfoList(List<JzdxCrimeInfoResultDTO> crimeInfoList, List<DictSelectResultDTO> crimeNameList, List<DictSelectResultDTO> crimeTypeList) {
        if (CollectionUtils.isEmpty(crimeInfoList) || CollectionUtils.isEmpty(crimeNameList) || CollectionUtils.isEmpty(crimeTypeList)) {
            return;
        }

        Map<String, DictSelectResultDTO> crimeNameMap = crimeNameList.stream().collect(Collectors.toMap(DictSelectResultDTO::getCode, v -> v));
        Map<String, DictSelectResultDTO> crimeTypeMap = crimeTypeList.stream().collect(Collectors.toMap(DictSelectResultDTO::getCode, v -> v));
        crimeInfoList.forEach(crime -> {
            if (crimeNameMap.containsKey(crime.getJtzm())) {
                crime.setJtzmmc(crimeNameMap.get(crime.getJtzm()).getName());
            }

            if (crimeTypeMap.containsKey(crime.getFzlx())) {
                crime.setFzlxmc(crimeTypeMap.get(crime.getFzlx()).getName());
            }
        });
    }

    default JzdxResultDTO convertCrimeInfoList(JzdxResultDTO corrector, List<JzdxCrimeInfoResultDTO> crimeInfoList, List<DictSelectResultDTO> crimeNameList, List<DictSelectResultDTO> crimeTypeList) {
        if (Objects.isNull(corrector) || CollectionUtils.isEmpty(crimeInfoList) || CollectionUtils.isEmpty(crimeNameList) || CollectionUtils.isEmpty(crimeTypeList)) {
            return corrector;
        }

        convertCrimeInfoList(crimeInfoList, crimeNameList, crimeTypeList);

        Map<String, List<JzdxCrimeInfoResultDTO>> crimeInfoListMap = crimeInfoList.stream()
                .collect(Collectors.groupingBy(JzdxCrimeInfoResultDTO::getRybh));

        if (crimeInfoListMap.containsKey(corrector.getRybh())) {
            corrector.setCrimeInfoList(crimeInfoListMap.get(corrector.getRybh()));
        }

        return corrector;
    }

}
